import random
from typing import List

color_list = ['方片','梅花','红桃','黑桃']
number_to_point = {'A':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'10':10,'J':0.5,'Q':0.5,'K':0.5}
number_list = ['2','3','4','5','6','7','8','9','10','J','Q','K','A']
type_list = ['无','对子','清一色','拖拉机','同花顺','炸弹']

class Card():
    def __init__(self, color, number):
        self.color = color
        self.number = str(number)
        
    def __lt__(self, other):
        
        if self.color =='王' and other.color == '王':
            return self.number < other.number
        elif self.color =='王':
            return False
        elif other.color == '王':
            return True
        
        else:
            if number_list.index(self.number) == number_list.index(other.number):
                return color_list.index(self.color) < color_list.index(other.color)
            else:
                return number_list.index(self.number) < number_list.index(other.number)
            
    def __gt__(self, other):
        return not self.__lt__(other)
        
        
    def __repr__(self):
        return self.color+self.number
    
    def __str__(self):
        return self.color+self.number
    
joker_1, joker_0 = Card('王','1'), Card('王','0')

class Cards():
    def __init__(self):
        self.cards = []
        self.p = 0
        self.add_cards()
        
    def add_cards(self):
        self.cards.append(Card('王','1'))
        self.cards.append(Card('王','0'))
        for c in color_list:
            for n in number_list:
                self.cards.append(Card(c,n))
        random.shuffle(self.cards)
        
    def deal_cards(self, number):
        left, right= self.p, self.p+number
        if self.p+number <= len(self.cards):
            self.p += number
            return self.cards[left:right]
        else:
            assert "牌堆剩余张数不够"
    
    def refresh_cards(self):
        self.cards = []
        self.p = 0
        self.add_cards()

    def __len__(self):
        return len(self.cards - self.p)
            
    def __repr__(self):
        return '牌堆剩余'+str(len(self.cards)-self.p)+'张牌'
    
    def __str__(self):
        return '牌堆剩余'+str(len(self.cards)-self.p)+'张牌'

class Game1():
    def cmp(card1: Card, card2: Card) -> int:
        if card1 < card2:
            return 0
        else:
            return 1

class Game2():
    def __init__(self, cards_list: List[Card]):
        if len(cards_list)!=2:
            assert "请检查牌张数，现为"+str(len(cards_list))+"张，应为2张"
            
        self.cards_list = cards_list.copy()
        self.value = None
        sum_point = 0
        
        self.cards_list.sort()
        
        if joker_1 in self.cards_list and joker_0 in self.cards_list:
            sum_point = 10.5
            self.value = [sum_point, joker_1]
        elif joker_1 in self.cards_list or joker_0 in self.cards_list:
            if self.cards_list[1].number == '10':
                sum_point = 10.5
                self.value = [sum_point, self.cards_list[1]]
            elif self.cards_list[1].number in ['J','Q','K']:
                sum_point = 10.5
                self.value = [sum_point, self.cards_list[0]]
            else:
                sum_point = 10
                self.value = [sum_point, self.cards_list[0]]
                
        else:
            sum_point = number_to_point[self.cards_list[0].number] + number_to_point[self.cards_list[1].number]
            if self.cards_list[0].number in ['J','Q','K'] and self.cards_list[1].number not in ['J','Q','K']:
                self.value = [sum_point, self.cards_list[1]]
            else:
                self.value = [sum_point, self.cards_list[0]]
        
        if self.value == None:
            assert "请检查2算法"
                
        def __lt__(self, other):
            if self.value[0] < other.value[0]:
                return True
            elif self.value[0] > other.value[0]:
                return False
            else:
                return self.value[1] < other.value[1]
            
        def __gt__(self, other):
            return not self.__lt__(other)

class Game3():
    
    def __init__(self, cards_list: List[Card]):
        if len(cards_list)!=3:
            assert "请检查牌张数，现为"+str(len(cards_list))+"张，应为3张"
            
        self.cards_list = cards_list.copy()
        self.value = None

        self.cards_list.sort()
        
        if joker_1 in self.cards_list and joker_0 in self.cards_list:
            self.value = ['炸弹', self.cards_list[2]]
        
        elif joker_1 in self.cards_list or joker_0 in self.cards_list:
            if self.cards_list[1].number == self.cards_list[2].number:
                self.value = ['炸弹', self.cards_list[1]]
            elif number_list.index(self.cards_list[1].number) - number_list.index(self.cards_list[2].number) == 1 or\
            number_list.index(self.cards_list[1].number) - number_list.index(self.cards_list[2].number) == 2:
                self.value = ['拖拉机', self.cards_list[1]]
                if self.cards_list[1].color == self.cards_list[2].color:
                    self.value[0] = ['同花顺']
            elif self.cards_list[1].color == self.cards_list[2].color:
                self.value ['清一色', self.cards_list[1]]
            else:
                self.value ['对子', self.cards_list[1]]
            
        else:
            if self.cards_list[0].number == self.cards_list[1].number and self.cards_list[1].number == self.cards_list[2].number:
                self.value = ['炸弹', self.cards_list[0]]
            elif number_list.index(self.cards_list[0].number) - number_list.index(self.cards_list[1].number) ==1 and\
            number_list.index(self.cards_list[1].number) - number_list.index(self.cards_list[2].number) ==1:
                self.value = ['拖拉机', self.cards_list[0]]
                if self.cards_list[0].color == self.cards_list[1].color and self.cards_list[1].color == self.cards_list[2].color:
                    self.value[0] = ['同花顺'] 
            elif self.cards_list[0].color == self.cards_list[1].color and self.cards_list[1].color == self.cards_list[2].color:
                self.value = ['清一色', self.cards_list[0]]
            elif self.cards_list[0].number == self.cards_list[1].number:
                self.value = ['对子', self.cards_list[0]]
            elif self.cards_list[1].number == self.cards_list[2].number:
                self.value = ['对子', self.cards_list[1]]
            else: 
                self.value = ['无', self.cards_list[0]]
                
        if self.value == None:
            assert "请检查3算法"
                
    def __lt__(self, other):
        if type_list.index(self.value[0]) < type_list.index(other.value[0]):
            return True
        elif type_list.index(self.value[0]) > type_list.index(other.value[0]):
            return False
        else:
            return self.value[1] < other.value[1]
        
            
    def __gt__(self, other):
        return not self.__lt__(other)      
